public class ThreadDemo_join {
    public static void main(String[] args) throws InterruptedException {
        // 线程1
        Thread thread1 = new Thread(new Runnable() {
            @Override
            public void run() {
                System.out.println("线程1开始执行...");
                for (; ;) {
                }
            }
        });
        // 获取主线程
        final Thread mainThread = Thread.currentThread();

        // 线程2
        Thread thread2 = new Thread(new Runnable() {
            @Override
            public void run() {
                // 休眠1s
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                // 中断主线程
                mainThread.interrupt();
            }
        });

        // 启动子线程1
        thread1.start();

        //延迟1s启动线程2
        thread2.start();

        try { // 等待线程1执行结束
            thread1.join();
        }catch (InterruptedException e) {
            System.out.println("main thread:" + e);
        }
    }
}
